package sns.action;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import sns.bean.Impression;
import sns.bean.Mblog;
import sns.bean.Tag;
import sns.bean.User;
import sns.service.ImpressionService;
import sns.service.MblogService;
import sns.service.RecommendService;
import sns.service.RelationService;
import sns.service.TagService;
import sns.service.UserService;
import sns.util.RecommendUser;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class GoToMainAction extends ActionSupport {

	// injection
	private MblogService mblogService;
	private UserService userService;
	private RelationService relationService;
	private RecommendService recommendService;
	private ImpressionService impressionService;
	private TagService tagService;
	// pass in
	// in ?id=xxx
	private Long userId;
	private User actualUser;
	// return
	private List<Mblog> mblogList;
	private List<User> recentFollowerList;
	private List<User> recentFolloweeList;
	private List<User> hotList;
	private User currentUser;
	private Integer numberOfFollowers;
	private Integer numberOfFollowees;
	private Integer numberOfBlogs;

	private List<Impression> impressionList;
	private List<Tag> tagList;
	private List<RecommendUser> recommendList;
	private Boolean isFan;
	private Boolean isFollowed;

	// parameter

	@Transactional
	public String execute() {

		// System.out.println("User id is "+ userId);
		// my own page
		if (userId == null) {
			currentUser = userService.getCurrentUser();
			if(currentUser==null){
				return Action.ERROR;
			}
			userId = currentUser.getIduser();
			actualUser =userService.findUserById(userId); 
			// get all the blogs including mine and followees
			mblogList = mblogService.getFolloweeMblogs(currentUser);

		}
		// other user's page
		else {
			// find user by id
			currentUser = userService.findUserById(userId);
			userId = userService.getCurrentUser().getIduser();
			actualUser =userService.findUserById(userId); 
			mblogList = mblogService.getUserMblogs(currentUser);
		}

		User visitUser = userService.findUserById(userId);

		// need to be sure they are chronological
		recentFolloweeList = userService.getNFollowees(currentUser, 5);
		recentFollowerList = userService.getNFollowers(currentUser, 5);
		hotList = userService.getHotUsers(10);
		numberOfBlogs = mblogService.getMblogNum(currentUser);
		numberOfFollowees = relationService.getFolloweeNum(currentUser);
		numberOfFollowers = relationService.getFollowerNum(currentUser);
		recommendList = recommendService.recommendUsers(currentUser, 10);
		impressionList = impressionService.getImpressionsOfUser(currentUser);
		tagList = tagService.getTagsOfUser(currentUser);
		isFollowed = relationService.checkIfFollow( visitUser,currentUser);
		isFan = relationService.checkIfFollow(currentUser,visitUser );

		return Action.SUCCESS;
	}

	public Boolean getIsFan() {
		return isFan;
	}

	public Boolean getIsFollowed() {
		return isFollowed;
	}

	public List<Impression> getImpressionList() {
		return impressionList;
	}

	public List<Tag> getTagList() {
		return tagList;
	}

	public List<RecommendUser> getRecommendList() {
		return recommendList;
	}

	public void setRecommendService(RecommendService recommendService) {
		this.recommendService = recommendService;
	}

	public void setImpressionService(ImpressionService impressionService) {
		this.impressionService = impressionService;
	}

	public void setTagService(TagService tagService) {
		this.tagService = tagService;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public List<Mblog> getMblogList() {
		return mblogList;
	}

	public List<User> getRecentFollowerList() {
		return recentFollowerList;
	}

	public List<User> getRecentFolloweeList() {
		return recentFolloweeList;
	}

	public List<User> getHotList() {
		return hotList;
	}

	public User getCurrentUser() {
		return currentUser;
	}

	public Integer getNumberOfFollowers() {
		return numberOfFollowers;
	}

	public Integer getNumberOfFollowees() {
		return numberOfFollowees;
	}

	public Integer getNumberOfBlogs() {
		return numberOfBlogs;
	}

	public void setMblogService(MblogService mblogService) {
		this.mblogService = mblogService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setRelationService(RelationService relationService) {
		this.relationService = relationService;
	}

	public void setActualUser(User actualUser) {
		this.actualUser = actualUser;
	}

	public User getActualUser() {
		return actualUser;
	}

}
